package com.donn.securitycontrolplatform.userinfo.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.donn.securitycontrolplatform.common.Result;
import com.donn.securitycontrolplatform.db2.mapper.UserinfoMapper;
import com.donn.securitycontrolplatform.userinfo.entity.Userinfo;
import com.donn.securitycontrolplatform.userinfo.service.IUserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhenglx
 * @since 2022-07-05
 */
@Service
public class UserinfoServiceImpl extends ServiceImpl<UserinfoMapper, Userinfo> implements IUserinfoService {

    @Autowired
    UserinfoMapper userinfoMapper;

    @Override
    @DS("db2")
    public Result login(String username, String password) {
        // 调用userMapper的findByUsername()方法，根据参数username查询用户数据
        QueryWrapper<Userinfo> queryWapper = new QueryWrapper<>();
        queryWapper.lambda().eq(Userinfo::getUsername, username);
        Userinfo result = userinfoMapper.selectOne(queryWapper);
        // 判断查询结果是否为null
        if (result == null) {
            return Result.init(500, "该用户不存在", null);
        }
        // 判断查询结果中的密码，得到的密码是否不一致
        if (!result.getPassword().equals(password)) {
            // 是：抛出PasswordNotMatchException异常
            return Result.init(500, "密码输入错误", null);
        }
        return Result.success(result);
    }
}
